package logic

import (
	"context"
	jwt "github.com/golangblogs/gojwt"
	"house/houseclient"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginLogic) Login(req *types.LoginRequest) (resp *types.Response, err error) {
	// todo: 登录
	login, err := l.svcCtx.HouseRpc.Login(l.ctx, &houseclient.LoginReq{
		Account:  req.Account,
		PassWord: req.PassWord,
		SmsCode:  req.SmsCode,
	})
	if err != nil {
		return &types.Response{
			Code: 500,
			Msg:  err.Error(),
			Data: nil,
		}, nil
	}
	claims := jwt.CustomClaims{ID: uint(login.Id)}
	newJWT := jwt.NewJWT("token")
	token, err := newJWT.CreateToken(claims)
	if err != nil {
		return &types.Response{
			Code: 500,
			Msg:  "登陆失败",
			Data: nil,
		}, nil
	}
	return &types.Response{
		Code: 200,
		Msg:  "登陆成功",
		Data: token,
	}, nil
}
